package com.sline.smis.web;

import com.sline.smis.jdbc.dao.StudentDao;
import com.sline.smis.jdbc.dao.impl.StudentDaoImpl;
import com.sline.smis.jdbc.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentDao studentDao = new StudentDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");

        String operate = req.getParameter("operate");
        operate = operate != null ? operate.trim() : "";
        Enumeration<String> parameters = req.getParameterNames();
        while (parameters.hasMoreElements()) {
            String key = parameters.nextElement();
            System.out.println(key + ": " + req.getParameter(key));
        }

        System.out.println("进入了service方法");
        System.out.println("----------- request" + operate);

        if ("delete".equals(operate)) {
            this.delete(req, resp);
        } else if ("update".equals(operate)) {
            this.update(req, resp);
        } else if ("edit".equals(operate)) {
            this.edit(req, resp);
        } else {
            this.list(req, resp);
        }
    }

    //删除操作
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        if (sid != null && !"".equals(sid.trim())) {
            studentDao.delete(Long.valueOf(sid.trim()));
        }

        resp.sendRedirect("/smis/student?operate=list");
    }

    // 添加/修改操作
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        Student student = new Student();
        student.setName(req.getParameter("name"));
        student.setAge(Integer.valueOf(req.getParameter("age")));
        boolean hasStudent = false;
        if (sid != null && !"".equals(sid.trim())) {
            Student t_student = studentDao.get(Long.valueOf(sid.trim()));
            if (t_student != null) {
                hasStudent = true;
            }
        }
        if (hasStudent) {
            studentDao.update(Long.valueOf(sid.trim()), student);
        } else {
            studentDao.save(student);
        }

        resp.sendRedirect("/smis/student?operate=list");
    }

    // 跳转到编辑/添加页面
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("sid") != null) {
            Student student = studentDao.get(Long.valueOf(req.getParameter("sid").trim()));

            req.setAttribute("student", student);
        }

        req.getRequestDispatcher("/WEB-INF/views/student/student_edit.jsp").forward(req, resp);
    }

    // 跳转到列表页
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> arr = studentDao.list();
        req.setAttribute("studentArr", arr);

        req.getRequestDispatcher("/WEB-INF/views/student/student_list.jsp").forward(req, resp);
    }
}
